<template>
  <commonBox title="个人资产搜索" width="1035px">
    <div class="search-container">
      <el-input
        class="search-input"
        v-model="seachMessage"
        @keyup.enter="search"
      ></el-input>
      <el-button type="primary" @click="search" class="search-btn"
        >搜索</el-button
      >
    </div>
    <div class="no-message" v-if="personList.length == 0">
      <img src="@/assets/img/nouser.png" alt="" />
    </div>
    <div class="result-container" v-else>
      <div class="person-content" v-infinite-scroll="load">
        <div
          class="person-item"
          v-for="item in personList"
          @click="searchThis(item)"
          :class="{
            'person-item-active': item.id == currentPerson.id,
          }"
        >
          <div class="item-text">{{ item.departmentname }}</div>
          <div class="item-text">{{ item.lastname }}</div>
        </div>
      </div>
      <div class="assets-content">
        <div class="assets-header">
          <div
            class="header-text"
            v-for="item in tableTitle"
            :style="`width:${item[1]}`"
          >
            {{ item[0] }}
          </div>
        </div>
        <div class="assets-list" v-infinite-scroll="loadAssest">
          <div class="list-item" v-for="item in assetsList">
            <div class="item-text" style="width: 10%">{{ item.lastname }}</div>
            <div class="item-text" style="width: 15%">
              {{ item.departmentname }}
            </div>
            <div class="item-text" style="width: 15%">{{ item.zcmc }}</div>
            <div class="item-text" style="width: 15%">{{ item.pp }}</div>
            <div class="item-text" style="width: 45%">{{ item.bz }}</div>
          </div>
        </div>
      </div>
    </div>
  </commonBox>
</template>

<script setup>
import { ref } from "vue";
import commonBox from "../commonBox.vue";
import { searchUser, getAssestList } from "@/api/index";
import { ElMessage } from "element-plus";
const seachMessage = ref("");
const personList = ref([]);
const size = 10;
const page = ref(1);
const assetsPage = ref(1);
const assetsList = ref([]);
const assetsTotal = ref(0);
const totalPage = ref(0);
const currentPerson = ref({});
const tableTitle = ref([
  ["姓名", "10%"],
  ["部门", "15%"],
  ["资产名称", "15%"],
  ["品牌", "15%"],
  ["备注", "45%"],
]);
function search() {
  page.value = 1;
  personList.value = [];
  assetsList.value = [];
  currentPerson.value = {};
  assetsTotal.value = 0;
  totalPage.value = 0;
  if (seachMessage.value == "") {
    ElMessage("请输入姓名");
    return;
  }
  searchUser({ keyword: seachMessage.value, page: page.value, size }).then(
    ({ data: res }) => {
      personList.value = res.data.list;
      totalPage.value = Math.ceil(res.data.total / size);
      page.value++;
    }
  );
}
function searchThis(item) {
  if (Object.keys(currentPerson.value).length === 0) {
    currentPerson.value = item;
  } else {
    if (currentPerson.value.id === item.id) {
      ElMessage("已选择该人员");
      return;
    } else {
      currentPerson.value = item;
    }
  }
  assetsList.value = [];
  assetsPage.value = 1;
  assetsTotal.value = 0;
  getAssestList({ page: assetsPage.value, size, user_id: item.id }).then(
    ({ data: res }) => {
      assetsList.value = res.data.list;
      assetsTotal.value = Math.ceil(res.data.total / size);
      assetsPage.value++;
    }
  );
}
function load() {
  if (
    seachMessage.value == "" ||
    personList.value.length == 0 ||
    page.value > totalPage.value
  ) {
    return;
  }
  searchUser({
    keyword: seachMessage.value,
    page: page.value,
    size,
  }).then(({ data: res }) => {
    if (res.data.list.length == 0) {
      return;
    }
    personList.value = [...personList.value, ...res.data.list];
    page.value++;
  });
}
function loadAssest() {
  if (
    assetsTotal.value == 0 ||
    assetsList.value.length == 0 ||
    assetsPage.value > assetsTotal.value
  ) {
    return;
  }
  getAssestList({
    page: assetsPage.value,
    size,
    user_id: assetsList.value[0].glr,
  }).then(({ data: res }) => {
    if (res.data.list.length == 0) {
      return;
    }
    assetsList.value = [...assetsList.value, ...res.data.list];
    assetsPage.value++;
  });
}
</script>

<style scoped lang="less">
.search-container {
  width: 100%;
  height: 57px;
  display: flex;
  align-items: center;
  justify-content: center;
  .search-input {
    width: 320px;
    height: 40px;
    margin-right: 15px;
  }
  .search-btn {
    width: 65px;
    height: 40px;
  }
}
.no-message {
  width: 100%;
  height: 600px;
  display: flex;
  align-items: center;
  justify-content: center;
  img {
    width: 512px;
    height: 512px;
  }
}
.result-container {
  width: 100%;
  padding: 0 24px;
  height: 600px;
  .person-content {
    width: 100%;
    height: 150px;
    overflow-y: scroll;
    display: flex;
    align-items: center;
    justify-content: space-evenly;
    flex-wrap: wrap;
    .person-item {
      width: 180px;
      height: 50px;
      border: 1px solid #000;
      display: flex;
      padding: 5px;
      align-items: center;
      justify-content: space-between;
      border-radius: 5px;
      cursor: pointer;
      margin-top: 5px;
      &:hover {
        border: 1px solid #4457fc;
        color: #4457fc;
      }
      .item-text {
        font-size: 15px;
        font-weight: 500;
      }
    }
    .person-item-active {
      border: 1px solid #4457fc;
      color: #4457fc;
    }
  }
  .assets-content {
    width: 100%;
    height: 450px;
    display: flex;
    flex-direction: column;
    .assets-header {
      width: 100%;
      height: 50px;
      display: flex;
      align-items: center;
      justify-content: center;
      border-bottom: 2px solid #e5e7eb;
      .header-text {
        font-size: 18px;
        font-weight: 600;
      }
    }
    .assets-list {
      width: 100%;
      height: 400px;
      overflow: scroll;
      .list-item {
        border-bottom: 1px solid #e5e7eb;
        margin-top: 15px;
        font-size: 16px;
        font-weight: 600;
        display: flex;
        align-items: center;
        justify-content: center;
      }
    }
  }
}
</style>
